<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\massage\model;

class CouponAtv extends \app\BaseModel
{
    protected $name = "massage_service_coupon_atv";
    protected $append = ["coupon"];
    public function getCouponAttr($value, $data)
    {
        if(!empty($data["id"])) {
            $coupom_model = new Coupon();
            $dis = ["a.status" => 1, "b.atv_id" => $data["id"]];
            $list = $coupom_model->alias("a")->join("massage_service_coupon_atv_coupon b", "b.coupon_id = a.id")->where($dis)->field("a.*,b.num,b.coupon_id")->group("b.coupon_id")->order("a.top desc,id desc")->select()->toArray();
            return $list;
        }
    }
    public function dataAdd($data)
    {
        $service = $data["coupon"];
        unset($data["coupon"]);
        $res = $this->insert($data);
        $id = $this->getLastInsID();
        $this->updateSome($id, $data["uniacid"], $service);
        return $id;
    }
    public function dataUpdate($dis, $data)
    {
        if(isset($data["coupon"])) {
            $service = $data["coupon"];
            unset($data["coupon"]);
        }
        $res = $this->where($dis)->update($data);
        if(!empty($service)) {
            $id = $this->where($dis)->value("id");
            $this->updateSome($id, $data["uniacid"], $service);
        }
        return $res;
    }
    public function updateSome($id, $uniacid, $coupon)
    {
        $s_model = new CouponAtvCoupon();
        $s_model->where(["atv_id" => $id])->delete();
        if(!empty($coupon)) {
            foreach ($coupon as $value) {
                $insert["uniacid"] = $uniacid;
                $insert["atv_id"] = $id;
                $insert["coupon_id"] = $value["id"];
                $insert["num"] = $value["num"];
                $s_model->dataAdd($insert);
            }
        }
        return true;
    }
    public function dataList($dis, $page)
    {
        $data = $this->where($dis)->order("top desc,id desc")->paginate($page)->toArray();
        return $data;
    }
    public function dataInfo($dis)
    {
        $data = $this->where($dis)->find();
        if(empty($data)) {
            $this->insert($dis);
            $data = $this->where($dis)->find();
        }
        return !empty($data) ? $data->toArray() : [];
    }
    public function invUser($user_id, $record_id)
    {
        $atv_record_model = new CouponAtvRecord();
        $atv_record_list_model = new CouponAtvRecordList();
        $record = $atv_record_model->dataInfo(["id" => $record_id]);
        \think\facade\Db::startTrans();
        if(!empty($record) && time() < $record["end_time"] && $record["status"] == 1) {
            $insert = ["uniacid" => $record["uniacid"], "user_id" => $record["user_id"], "to_inv_id" => $user_id, "record_id" => $record_id];
            $res = $atv_record_list_model->dataAdd($insert);
            if(!empty($res["code"])) {
                \think\facade\Db::rollback();
                return ["code" => 500, "msg" => "登陆失败，请刷新重试"];
            }
            if($record["to_inv_user"] == 1 && !empty($record["coupon"])) {
                $res = $this->giveAtvCoupon($record_id, $user_id);
                if(!empty($res["code"])) {
                    \think\facade\Db::rollback();
                    return $res;
                }
            }
            $res = $this->recordSuccess($record_id);
            if(!empty($res["code"])) {
                \think\facade\Db::rollback();
                return $res;
            }
        }
        \think\facade\Db::commit();
        return true;
    }
    public function giveAtvCoupon($record_id, $user_id)
    {
        $atv_record_model = new CouponAtvRecord();
        $record_model = new CouponRecord();
        $atv_record_coupon_model = new CouponAtvRecordCoupon();
        $record = $atv_record_model->dataInfo(["id" => $record_id]);
        foreach ($record["coupon"] as $value) {
            $num = $value["num"];
            if(0 < $num) {
                $res = $record_model->recordAdd($value["coupon_id"], $user_id, $num);
                if($res == 0) {
                    return ["code" => 500, "msg" => "登陆失败，请刷新重试"];
                }
                $insert = ["uniacid" => $record["uniacid"], "user_id" => $user_id, "atv_id" => $record["atv_id"], "coupon_id" => $value["coupon_id"], "num" => $value["num"], "status" => 2, "success_num" => $num];
                $res = $atv_record_coupon_model->dataAdd($insert);
                if($res == 0) {
                    return ["code" => 500, "msg" => "登陆失败，请刷新重试"];
                }
            }
        }
        return true;
    }
    public function recordSuccess($record_id)
    {
        $atv_record_list_model = new CouponAtvRecordList();
        $atv_record_model = new CouponAtvRecord();
        $have_num = $atv_record_list_model->where(["record_id" => $record_id])->count();
        $record = $atv_record_model->dataInfo(["id" => $record_id]);
        if($record["inv_user_num"] <= $have_num) {
            $res = $atv_record_model->dataUpdate(["id" => $record["id"], "status" => 1], ["status" => 2]);
            if($res == 0) {
                return ["code" => 500, "msg" => "登陆失败，请刷新重试"];
            }
            if($record["inv_user"] == 1) {
                $res = $this->giveAtvCoupon($record_id, $record["user_id"]);
                if(!empty($res["code"])) {
                    return $res;
                }
            }
        }
        return true;
    }
}

?>